package com.micromap.dataprocessing

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.common.io.Files
import com.micromap.dataprocessing.domain.AuthPropertyValue
import com.micromap.dataprocessing.domain.Button
import com.micromap.dataprocessing.repository.AuthPropertyRepository
import com.micromap.dataprocessing.repository.AuthPropertyValueRepository
import com.micromap.dataprocessing.repository.MenuNodeRepository
import com.micromap.dataprocessing.repository.MenuRepository
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import java.io.File
import java.io.IOException
import java.time.LocalDate


@RunWith(SpringRunner::class)
@SpringBootTest
class DataProcessingApplicationTests {
    @Autowired
    lateinit var authPropertyRepository: AuthPropertyRepository
    @Autowired
    lateinit var authPropertyValueRepository: AuthPropertyValueRepository
    @Autowired
    lateinit var menuRepository: MenuRepository
    @Autowired
    lateinit var menuNodeRepository: MenuNodeRepository

    @Test
    fun genButtons() {
        val buttons: MutableList<Button>
        val authPropertyValues: MutableList<AuthPropertyValue> = arrayListOf()
        val objectMapper = ObjectMapper()
        objectMapper.findAndRegisterModules()
        try {
            val fileName = "buttons_huanbao"
            buttons = objectMapper.readValue(javaClass.classLoader.getResource(fileName + ".json"), object : TypeReference<List<Button>>() {})
            for (i in buttons.indices) {
                val a = AuthPropertyValue()
                a.propertyValueId = authPropertyValues.size.toLong()
                a.propertyId = 10000000
                a.name = buttons[i].name
                a.value = buttons[i].code
                a.parentId = null
                a.enabled = 1
                a.orderId = 1
                a.sourceId = "1000062"
                a.version = 1L
                a.crtDate = LocalDate.now()
                a.crtUserId = "admin"
                a.crtUserName = "admin"
                a.mntDate = LocalDate.now()
                a.mntUserId = "admin"
                a.mntUserName = "admin"
                authPropertyValues.add(a)
                for (j in buttons[i].button.indices) {
                    val a0 = AuthPropertyValue()
                    a0.propertyValueId = (authPropertyValues.size).toLong()
                    a0.propertyId = 10000000
                    a0.name = buttons[i].button[j].name
                    a0.value = buttons[i].button[j].code
                    a0.parentId = null
                    a0.enabled = 1
                    a0.orderId = 1
                    a0.sourceId = "100062"
                    a0.version = 1L
                    a0.crtDate = LocalDate.now()
                    a0.crtUserId = "admin"
                    a0.crtUserName = "admin"
                    a0.mntDate = LocalDate.now()
                    a0.mntUserId = "admin"
                    a0.mntUserName = "admin"
                    a0.extendCode = buttons[i].code
                    authPropertyValues.add(a0)
                }
                val backup = AuthPropertyValue()
                backup.propertyValueId = (authPropertyValues.size).toLong()
                backup.propertyId = 10000000
                backup.name = "备用"
                backup.value = "Backup"
                backup.parentId = null
                backup.enabled = 1
                backup.orderId = 1
                backup.sourceId = "100062"
                backup.version = 1L
                backup.crtDate = LocalDate.now()
                backup.crtUserId = "admin"
                backup.crtUserName = "admin"
                backup.mntDate = LocalDate.now()
                backup.mntUserId = "admin"
                backup.mntUserName = "admin"
                backup.extendCode = buttons[i].code
                authPropertyValues.add(backup)
            }
            var sql = ""
            authPropertyValues.forEach({
                sql += "INSERT INTO T_AAA_AUTH_PROPERTY_VALUE VALUES (s_aaa_auth_property_value.nextval, 10000000, '${it.name}', '${it.value}', NULL, 1, 1, '100062', 1, 'admin', 'admin', TO_TIMESTAMP('201712120000000000', 'YYYYMMDDHH24MISSFF4'), 'admin', 'admin', TO_TIMESTAMP('201712120000000000', 'YYYYMMDDHH24MISSFF4'), '${if (it.extendCode == null) "" else it.extendCode}');\n"
            })
            val path = "/Users/limeng/Documents/$fileName.sql"
            val file = File(path)
            Files.createParentDirs(file)
            Files.write(sql.toByteArray(), file)
            //authPropertyValueRepository.save(authPropertyValues)
        } catch (e: IOException) {
            e.printStackTrace()
        }

        //Assert.assertTrue(authPropertyValueRepository.findAll().size > 0)
    }

    @Test
    fun genMenus(){

    }
}
